package cn.com.tianpeidong.restaurantadmin.common.util.page;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;

/**
 * @author tianpeidong
 * @version 创建时间: 19-5-1 下午11:00
 */
public class PageWrapper<T> {

    private static final Integer DEFAULT_PAGE_NUM = 1;

    private static final Integer MAX_PAGE_SIZE = 15;

    public PageWrapper() {
        this(MAX_PAGE_SIZE, DEFAULT_PAGE_NUM);
    }

    public PageWrapper(Integer pageSize, Integer pageNum) {
        if (pageNum == null || pageNum <= 0) {
            pageNum = DEFAULT_PAGE_NUM;
        }
        if (pageSize == null || pageSize <= 0 || pageSize > MAX_PAGE_SIZE) {
            pageSize = MAX_PAGE_SIZE;
        }
        PageHelper.startPage(pageNum, pageSize);
    }

    private long getTotal(List<T> list) {
        return new PageInfo<T>(list).getTotal();
    }

    public JSONObject getResult(List<T> list) {
        JSONObject result = new JSONObject();
        result.put("data", list);
        result.put("total", this.getTotal(list));
        return result;
    }

}
